<?if $(sys.BUILDARCH)="x86"?>
    <?define Win64 = "no" ?>
    <?define PlatformProgramFilesFolder = "ProgramFilesFolder" ?>
<?elseif $(sys.BUILDARCH)="x64"?>
    <?define Win64 = "yes" ?>
    <?define PlatformProgramFilesFolder = "ProgramFiles64Folder" ?>
<?else?>
    <?error Unsupported value of sys.BUILDARCH=$(sys.BUILDARCH)?>
<?endif?>

<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <Product
            Id="*"
            Name="{{{product_name}}}"
            UpgradeCode="{{{upgrade_code}}}"
            Language="1033"
            Codepage="1252"
            Manufacturer="{{{manufacturer}}}"
            Version="{{{version}}}">

        <Package Id="*"
                 Keywords="Installer"
                 InstallerVersion="450"
                 Languages="1033"
                 Compressed="yes"
                 InstallScope="perMachine"
                 SummaryCodepage="1252"/>

         <MajorUpgrade DowngradeErrorMessage="A newer version of [ProductName] is already installed."
                  MigrateFeatures="yes" />


        <Media Id="1" Cabinet="app.cab" EmbedCab="yes" />
        
        <WixVariable Id="WixUIBannerBmp" Value="{{{icon_path}}}" />
        
        <Icon Id="ProductIcon" SourceFile="{{{icon_path}}}"/>
        <Property Id="ARPPRODUCTICON" Value="ProductIcon" />
        <Property Id="ARPNOREPAIR" Value="yes" Secure="yes" />      <!-- Remove repair -->
        <SetProperty Id="ARPNOMODIFY" Value="1" After="InstallValidate" Sequence="execute"/>

        <UI>
            <Property Id="WIXUI_INSTALLDIR" Value="INSTALLDIR" />

            <!-- Skip license dialog -->
            <Publish Dialog="WelcomeDlg"
                     Control="Next"
                     Event="NewDialog"
                     Value="InstallDirDlg"
                     Order="2">1</Publish>
            <Publish Dialog="InstallDirDlg"
                     Control="Back"
                     Event="NewDialog"
                     Value="WelcomeDlg"
                     Order="2">1</Publish>
        </UI>

        <UIRef Id="WixUI_InstallDir" />

        <Directory Id="TARGETDIR" Name="SourceDir">
            <Directory Id="$(var.PlatformProgramFilesFolder)" Name="PFiles">
                <Directory Id="INSTALLDIR" Name="{{{product_name}}}"/>
            </Directory>
            <Directory Id="ProgramMenuFolder">
                <Directory Id="ApplicationProgramsFolder" Name="{{{product_name}}}"/>
            </Directory>
        </Directory>

        <DirectoryRef Id="INSTALLDIR">
            <Component Id="Path" Guid="{{{path_component_guid}}}" Win64="$(var.Win64)">
                <File Id="Path" Source="{{{app_exe_source}}}" KeyPath="yes" Checksum="yes"/>
            </Component>
            {{#each binaries as |bin| ~}}
            <Component Id="{{ bin.id }}" Guid="{{bin.guid}}" Win64="$(var.Win64)">
                <File Id="Path_{{ bin.id }}" Source="{{bin.path}}"  KeyPath="yes"/>
            </Component>
            {{/each~}}
            {{{resources}}}
            <Component Id="CMP_ReadFileShortcut"
                Guid="1AF06B42-CD42-4AED-959F-36DB5E512046">

                <Shortcut Id="UninstallShortcut"
						  Name="Uninstall {{{product_name}}}"
						  Description="Uninstalls {{{product_name}}}"
						  Target="[System64Folder]msiexec.exe"
						  Arguments="/x [ProductCode]" />

				<RemoveFolder Id="INSTALLDIR"
							  On="uninstall" />

				<RegistryValue Root="HKCR"
							   Key="Software\{{{manufacturer}}}\{{{product_name}}}"
							   Name="installed"
							   Type="integer"
							   Value="1"
							   KeyPath="yes" />
            </Component>
        </DirectoryRef>

        <DirectoryRef Id="ApplicationProgramsFolder">
            <Component Id="ApplicationShortcut" Guid="81ccebd8-b769-4bed-bdbd-0340f9f7cad1">
                <Shortcut Id="ApplicationStartMenuShortcut" 
                    Name="{{{product_name}}}"
                    Description="Runs {{{product_name}}}"
                    Target="[!Path]"
                    Icon="ProductIcon">
                    <ShortcutProperty Key="System.AppUserModel.ID" Value="{{{manufacturer}}}"/>
                </Shortcut>
                <RemoveFolder Id="ApplicationProgramsFolder" On="uninstall"/>
                <RegistryValue Root="HKCU" Key="Software\{{{manufacturer}}}\{{{product_name}}}" Name="installed" Type="integer" Value="1" KeyPath="yes"/>
           </Component>
        </DirectoryRef>

        <Feature
                Id="MainProgram"
                Title="Application"
                Description="Installs {{{product_name}}}."
                Level="1"
                ConfigurableDirectory="INSTALLDIR"
                AllowAdvertise="no"
                Display="expand"
                Absent="disallow">

            {{#each resource_file_ids as |resource_file_id| ~}}
                <ComponentRef Id="{{ resource_file_id }}"/>
            {{/each~}}

            <Feature Id="ShortcutsFeature"
                Title="Shortcuts"
                Level="1">
                <ComponentRef Id="Path"/>
                <ComponentRef Id="CMP_ReadFileShortcut" />
                <ComponentRef Id="ApplicationShortcut" />
            </Feature>

            <Feature
                Id="Environment"
                Title="PATH Environment Variable"
                Description="Add the install location of the [ProductName] executable to the PATH system environment variable. This allows the [ProductName] executable to be called from any location."
                Level="1"
                Absent="allow">
            <ComponentRef Id="Path"/>
            {{#each binaries as |bin| ~}}
            <ComponentRef Id="{{ bin.id }}"/>
            {{/each~}}
            </Feature>
        </Feature>

        <SetProperty Id="ARPINSTALLLOCATION" Value="[INSTALLDIR]" After="CostFinalize"/>

        {{#if embedded_server}}
        <InstallExecuteSequence>
            <Custom Action='LoopBackCmd' After='InstallFiles'/>
        </InstallExecuteSequence>
        {{/if}}
        
    </Product>

    {{#if embedded_server}}
    <Fragment>
        <CustomAction Id="LoopBackCmd" Directory="ApplicationProgramsFolder" Execute="commit" Impersonate="no" ExeCommand="cmd.exe /c &quot;CheckNetIsolation.exe LoopbackExempt -a -n=&quot;Microsoft.Win32WebViewHost_cw5n1h2txyewy&quot;&quot;" Return="ignore" />
    </Fragment>
    {{/if}}
</Wix>